package arconplugin.handlers;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class ResultVisualizer {
	private final String arcon_console_name = "ArconConsole";
	private boolean DEBUG = true; //TODO: disable this for release
	private MessageConsole console;
	private MessageConsoleStream consoleStream;
	
	public void debugPrint(String output) {
        if (DEBUG) {
            System.out.println(output);
        }
    }
    public void doPrint(String output) {
    	//System.out.println(output);
    	consoleStream.println(output);
    }
	
    public void printError(String error) {
        //System.err.println(error);
    	consoleStream.println(error);
    }
	
    //Constructor
    public ResultVisualizer() {
    	console = findConsole(arcon_console_name);
    	consoleStream = console.newMessageStream(); //TODO: is this valid, error free to keep it open all time? Might just be my c++ nerves.
    }
    
	private MessageConsole findConsole(String name) {
      ConsolePlugin plugin = ConsolePlugin.getDefault();
      IConsoleManager conMan = plugin.getConsoleManager();
      IConsole[] existing = conMan.getConsoles();
      for (int i = 0; i < existing.length; i++) {
         if (name.equals(existing[i].getName())) {
            return (MessageConsole) existing[i];
         }
      }
      //no console found, so create a new one
      MessageConsole myConsole = new MessageConsole(name, null);
      conMan.addConsoles(new IConsole[]{myConsole});
      return myConsole;
   }

}
